package baseModelHandler

import (
	"awesome-devops/applications/backend/modules/services"
	"awesome-devops/applications/models"
	awesomehttp "gitee.com/jjawesomejj/awesomehttp/system"
	"strconv"
)

type BaseModelHandler struct {
	awesomehttp.HttpBaseHandler
	BaseModel models.BaseModel
}

func (context *BaseModelHandler) ActionAdd() interface{} {
	projectName := context.HttpContext.PostOne("project_name").(string)
	gitUrl := context.HttpContext.PostOne("git_url").(string)
	description := context.HttpContext.PostOne("description").(string)
	branch := context.HttpContext.PostOne("branch").(string)
	project := models.Project{ProjectName: projectName,
		GitUrl:      gitUrl,
		Description: description,
		Branch:      branch,
	}
	return services.ProjectService{}.Create(project)
}

func (context *BaseModelHandler) ActionList() interface{} {
	page := context.HttpContext.PostOne("page", "").(string)
	pageSize := context.HttpContext.PostOne("pageSize", "").(string)
	keywords := context.HttpContext.PostOne("keywords", "").(string)
	pageInt, err := strconv.Atoi(page)
	if err != nil {
		pageInt = 1
	}
	pageSizeInt, err := strconv.Atoi(pageSize)
	if err != nil {
		pageSizeInt = 30
	}

	return services.ProjectService{}.List(keywords, pageInt, pageSizeInt)
}
